package com.jadesoft.ciadapter.library;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;

import com.jadesoft.ciadapter.plugin.base.DefaultAction;
import com.jadesoft.ciadapter.plugin.base.PluginManager;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class JdPageHelper {
	ServletRequest request ;
	Map<String,String> meta ;
	Css css ;
	Script script ;
	private JdPageHelper(){}
	private JdPageHelper(ServletRequest request){
		this.request = request ;
		this.meta = defaultMeta();
		this.css = new Css();
		request.setAttribute("css", this.css);
		this.script = new Script();
		request.setAttribute("script", this.script);
	}
	
	public static JdPageHelper getHelper(ServletRequest request){
		Object helper = request.getAttribute("jdpage_helper");
		if (helper == null){
			helper = new JdPageHelper(request);
			request.setAttribute("jdpage_helper", helper);
		}
		return (JdPageHelper)helper;
	}
	
	public void css_enqueue(String... args){
		for ( String href : args){
			this.css.enqueue(href);
		}
	}
	
	public void js_enqueue(String... args){
		for ( String href : args){
			this.script.enqueue(href);
		}
	}
	public String css_export(){
		return this.css.export();
	}
	
	public String js_export(){
		return this.script.export();
	}
	
	@SuppressWarnings({ "deprecation" })
	public static String loadTemplate(String tplPath,Map<String,Object> params,Writer output,ServletRequest req) throws TemplateException, IOException{
		if (params == null){
			params = new HashMap<String,Object>();
		}
		if ( ! params.containsKey("helper") && req != null){
			params.put("helper", JdPageHelper.getHelper(req));
		}
		Template temp = Configuration.getDefaultConfiguration().getTemplate(tplPath);
		if (output != null){
			temp.process(params, output);
			return null;
		} else {
			output = new StringWriter();
			temp.process(params, output);
			return output.toString();
		}
	}
	
	public void addAction(String tag,String returnValue){
		if (this.request != null){
			PluginManager.addAction(tag, new DefaultAction(returnValue), this.request);
		}
	}
	
	
	public String do_action(String tag,Object...args){
		return PluginManager.doAction(tag,this.request);
	}
	
	public String getPageMeta(String key){
		return this.meta.get(key);
	}
	
	public JdPageHelper setPageMeta(String key,String value){
		this.meta.put(key, value);
		return this;
	}
	@Override
	public String toString() {
		return "";
	}
	
	private Map<String,String> defaultMeta(){
		Map<String,String> map = new HashMap<String,String>();
		map.put("title", "");
		map.put("current_file", "");
		map.put("parent_file", "");
		map.put("contextual_help", "<a href=\"http://codex.wordpress.org/\" target=\"_blank\">Documentation</a><br><a href=\"http://wordpress.org/support/\" target=\"_blank\">Support Forums</a>");
		
		return map ;
		
	}
	

}
